﻿using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.Data;
using ArtMath.Resource;
using System;
using System.Drawing;
using ArtMath.CommonType;

namespace ArtMath.Component.CoordinateSystem
{
    /// <summary>
    /// X轴
    /// </summary>
    [Serializable]
    public class XAxis : RectangularCoordinateAxisBase
    {
        public XAxis(SketchPad pad) : base(pad, Resources.Translate("XAxis"), ToolTypeName.xz, false)
        {
        }
        protected override void Scale_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            base.Scale_ParametersValueChanged(sender, e);
            if (Scale.Value > 0)
            {
                Pad.Coordinate.CoordinateChanged(PointsRelocationReason.XAxisScaled);
            }
        }
        protected override void Scale_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            base.Scale_ExpressionSet(sender, e);
            if (Scale.Value > 0)
            {
                Pad.Coordinate.CoordinateChanged(PointsRelocationReason.XAxisScaled);
            }
        }
        protected override string getFormula()
        {
            if (Pad.CoordinateType == CoordinateSystemType.PolarCoordinate)
            {
                return "ρsin(θ)=0";//可能永远无法执行
            }
            else
            {
                return "y=0";
            }
        }
        public override void CreateComponents()
        {
            Slope = 0;
            base.CreateComponents();
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible)
            {
                double axisAngle = Core.Helper.Angle.StandardizeRad(linkedRC.RotateAngle.RadValue),
                    gridAngle = (axisAngle + Core.Helper.Angle.RightAngle) % Core.Helper.Angle.RadRound;
                SolidBrush SB = new SolidBrush(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? textColor.ReversedColor : textColor.Color);
                PointD loc = (linkedRC.OriginCoordinate + Pad.Location) / Pad.Zoom.ToPointD();
                SizeD size = Pad.CanvasPSize;
                PointD gridlen;
                Pen kdPen = new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? rulerColor.ReversedColor : rulerColor.Color, 2),
                    gridPen = new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? gridColor.ReversedColor : gridColor.Color, 1);
                if (!linkedRC.YAxis.Visible)//如果隐藏y轴则画一条x=0的网格线
                {
                    if (ShowGrid)
                    {
                        gridlen = Utility.CalcLength(loc, size, gridAngle * Core.Helper.Angle.RToD);
                        gra.DrawLine(gridPen, 0, (float)gridlen.X, 0, (float)-gridlen.Y);
                    }
                    gra.DrawLine(kdPen, 0, -6, 0, 6);
                }
                PointD axislen = Utility.CalcLength(loc, size, axisAngle * Core.Helper.Angle.RToD);
                double rulerCount = 0;
                while ((rulerCount + interval) * Scale.Value < axislen.Y - 17)
                {
                    rulerCount += interval;
                    gra.TranslateTransform((float)(interval * Scale.Value), 0);
                    if (ShowGrid)
                    {
                        gridlen = Utility.CalcLength(loc.POffset(axisAngle, rulerCount * Scale.Value), size, gridAngle * Core.Helper.Angle.RToD);
                        gra.DrawLine(gridPen, 0, (float)gridlen.X, 0, (float)-gridlen.Y);//画网格
                    }
                    if (ShowGraduation)
                    {
                        if (!rulerCount.IsInteger()) kdPen.Width = 1;
                        gra.DrawLine(kdPen, 0, -6, 0, 6);//画刻度
                        if (!rulerCount.IsInteger()) kdPen.Width = 2;
                    }
                    if (ShowGraduationText) gra.DrawString(FormatRuler(rulerCount),
                        new Font("Times New Roman", rulerCount.IsInteger() ? 16 : 10), SB,
                        rulerCount.IsInteger() ? new PointF(-9, GraduationTextLocation == AxisTextLocation.Below ? 6 : -29) :
                        new PointF(-5, GraduationTextLocation == AxisTextLocation.Below ? 8 : -22));
                }
                Max = rulerCount * Unit.Value;
                gra.TranslateTransform((float)(-rulerCount * Scale.Value), 0);
                if (linkedRC.ShowxOy)
                    gra.DrawString("x", new Font("Times New Roman", 20, FontStyle.Italic), SB, (float)(axislen.Y - 22), GraduationTextLocation == AxisTextLocation.Below ? -35 : 0);
                //x轴负方向
                rulerCount = 0;
                while ((interval - rulerCount) * Scale.Value < axislen.X)
                {
                    rulerCount -= interval;
                    gra.TranslateTransform((float)(-interval * Scale.Value), 0);
                    if (ShowGrid)
                    {
                        gridlen = Utility.CalcLength(loc.POffset(axisAngle, rulerCount * Scale.Value), size, gridAngle * Core.Helper.Angle.RToD);
                        gra.DrawLine(gridPen, 0, (float)gridlen.X, 0, (float)-gridlen.Y);//画网格
                    }
                    if (ShowGraduation)
                    {
                        if (!rulerCount.IsInteger()) kdPen.Width = 1;
                        gra.DrawLine(kdPen, 0, -6, 0, 6);//画刻度
                        if (!rulerCount.IsInteger()) kdPen.Width = 2;
                    }
                    if (ShowGraduationText) gra.DrawString(FormatRuler(rulerCount),
                        new Font("Times New Roman", rulerCount.IsInteger() ? 16 : 10), SB,
                        rulerCount.IsInteger() ? new PointF(-15, GraduationTextLocation == AxisTextLocation.Below ? 6 : -29) :
                        new PointF(-9, GraduationTextLocation == AxisTextLocation.Below ? 8 : -22));
                }
                Min = rulerCount * Unit.Value;
                gra.TranslateTransform((float)(-rulerCount * Scale.Value), 0);
                gra.DrawLine(LinePen, (float)-axislen.X, 0, (float)axislen.Y, 0);
            }
            //base.Paint(gra);
        }
    }
}
